package com.xrit.sunnyday.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.xrit.sunnyday.constant.MessageConstant;
import com.xrit.sunnyday.entity.PageResult;
import com.xrit.sunnyday.entity.QueryPageBean;
import com.xrit.sunnyday.entity.Result;
import com.xrit.sunnyday.pojo.Setmeal;
import com.xrit.sunnyday.service.SetmealService;
import com.xrit.sunnyday.utils.QiNiuUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * @author Daoying
 * creationTime: 2022/10/20 14:13
 */
@RestController
@RequestMapping("/setmeal")
public class SetmealController {

    @Reference
    private SetmealService setmealService;


    //套餐上传图片
    @PostMapping("/upload")
    public Result upload(MultipartFile imgFile) {
        //1.获取原有图片名称，截取到后缀名
        String originalFilename = imgFile.getOriginalFilename();
        String substring = originalFilename.substring(originalFilename.lastIndexOf("."));
        //生成唯一文件名，拼接后缀名
        String filename = UUID.randomUUID() + substring;
        //调用七牛上传文件
        try {
            QiNiuUtils.uploadViaByte(imgFile.getBytes(), filename);
            /*
            返回数据给页面
            {
                flag:
                message:
                data:{
                    imgName: 图片名,
                    domain: QiNiuUtils.DOMAIN
                }
            }
            */
            HashMap<String, String> map = new HashMap<>();
            map.put("imgName", filename);
            map.put("domain", QiNiuUtils.DOMAIN);
            return new Result(true, MessageConstant.PIC_UPLOAD_SUCCESS, map);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return new Result(false, MessageConstant.PIC_UPLOAD_FAIL);
    }

    //添加套餐
    @PostMapping("/add")
    public Result add(@RequestBody Setmeal setmeal, Integer[] checkgroupIds) {
        // 调用业务服务添加
        setmealService.add(setmeal, checkgroupIds);
        // 响应结果
        return new Result(true, MessageConstant.ADD_SETMEAL_SUCCESS);
    }

    //分页
    @PostMapping("/findPage")
    public Result findPage(@RequestBody QueryPageBean queryPageBean) {
        // 调用业务服务分页
        PageResult page = setmealService.findPage(queryPageBean);
        // 响应结果
        return new Result(true, MessageConstant.QUERY_SETMEAL_SUCCESS, page);
    }

    //删除
    @PostMapping("deleteById")
    public Result deleteById(int id) {
        // 调用业务服务删除
        setmealService.deleteById(id);
        // 响应结果
        return new Result(true, MessageConstant.DELETE_SETMEAL_SUCCESS);
    }

    //通过id查询套餐信息
    @GetMapping("/findById")
    public Result findById(int id) {
        // 调用服务查询
        Setmeal setmeal = setmealService.findById(id);
        // 前端要显示图片需要全路径
        // 封装到map中，解决图片路径问题
        Map<String, Object> resultMap = new HashMap<String, Object>();
        resultMap.put("setmeal", setmeal); // formData
        resultMap.put("domain", QiNiuUtils.DOMAIN); // domain
        return new Result(true, MessageConstant.QUERY_SETMEAL_SUCCESS, resultMap);
    }

    //通过id查询选中的检查组ids
    @GetMapping("/findCheckgroupIdsBySetmealId")
    public Result findCheckgroupIdsBySetmealId(int id) {
        List<Integer> checkgroupIds = setmealService.findCheckgroupIdsBySetmealId(id);
        return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS, checkgroupIds);
    }

    //修改
    @PostMapping("/update")
    public Result update(@RequestBody Setmeal setmeal, Integer[] checkgroupIds) {
        // 调用业务服务修改
        setmealService.update(setmeal, checkgroupIds);
        // 响应结果
        return new Result(true, MessageConstant.EDIT_SETMEAL_SUCCESS);
    }
}
